package com.mgy.common.excel;

import java.util.*;

/**
 * 行定义
 *
 * @author maguoyong
 * @since 2018/01/24
 */
public class DataRow {
    /**
     * 定义该行记录在table所处的行数
     */
    private int rowIndex;

    private DataColumnCollection columns;

    private DataTable table;
    /**
     * 列索引->列值
     */
    private List<Object> rowList = new ArrayList<>();


    public DataRow(DataTable table, DataColumnCollection columns, int rowIndex) {
        this.table = table;
        this.columns = columns;
        this.rowIndex = rowIndex;
    }

    public DataRow(DataTable table, DataColumnCollection columns) {
        this(table, columns, 0);
    }

    /**
     * 此行样式
     */
    private ExcelStyle rowStyle;

    public ExcelStyle getRowStyle() {
        return rowStyle;
    }

    public void setRowStyle(ExcelStyle rowStyle) {
        this.rowStyle = rowStyle;
    }


    /**
     * 获取当前行的行索引
     */
    public int getRowIndex() {
        return rowIndex;
    }

    /**
     * 功能描述： 获取当前行所属数据表对象
     */
    public DataTable getTable() {
        return this.table;
    }

    public DataColumnCollection getColumns() {
        return columns;
    }

    public void setValue(int columnIndex, Object value) {
        setValue(this.columns.get(columnIndex), value);
    }

    public void setValue(DataColumn column, Object value) {
        if (column != null) {
            rowList.add(column.getOrdinal(), value);
        }
    }

    public void setValues(Object... values) {
        Objects.requireNonNull(values, "列值不能为空");
        if (values.length != columns.size()) {
            throw new RuntimeException("列值和列数不对应");
        }
        for (int i = 0; i < values.length; i++) {
            setValue(i, values[i]);
        }
    }

    public Object getValue(int index) {
        return this.rowList.get(index);
    }

    public void setRowIndex(int rowIndex) {
        this.rowIndex = rowIndex;
    }


    public boolean getMerge() {
        return this.merge;
    }

    public void setMerge(boolean merge) {
        this.merge = merge;
    }

    /**
     * 是否合并行
     */
    private boolean merge = false;
}
